package com.example.fufu.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.fufu.common.R;
import com.example.fufu.modle.SendRequest;
import com.example.fufu.modle.tianqinewslist;
import com.example.fufu.service.UserServeice;
import com.example.fufu.util.HttpUtils;
import me.chanjar.weixin.mp.api.WxMpInMemoryConfigStorage;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
import org.springframework.stereotype.Service;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class UserServeiceImpl implements UserServeice {
    public static String tianxingkey = "91d44632da67f333474a2f567c6e861e";
    public static String chpurl = "http://api.tianapi.com/caihongpi/index?key=" + tianxingkey;
    public static String tianqiurl = "http://api.tianapi.com/tianqi/index?key=" + tianxingkey + "&city=深圳";

    @Override
    public R wechatPush() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String dateNow = sdf.format(new Date());
        String tainshu = tainshu(dateNow);
        //获取天气
        tianqinewslist tianQi = getTianQi(dateNow);
        System.out.println(JSON.toJSONString(tianQi));
        //获取彩虹屁
        String caihongpi = getCaiHongPi(chpurl);
        //发送
        return sendWeixin(dateNow, tianQi, caihongpi, tainshu);
    }


    /**
     * 微信模板处理
     *
     * @param dateNow
     * @param tianQi
     * @param caihongpi
     * @param tainshu
     * @param sendRequest
     * @return
     */
    public R sendWeixin(String dateNow, tianqinewslist tianQi, String caihongpi, String tainshu) {
        WxMpInMemoryConfigStorage wxStorage = new WxMpInMemoryConfigStorage();
        wxStorage.setAppId("wxbf96ac13c992cf1f");
        wxStorage.setSecret("7d10a14e1ba143d7ac8aa495e8a41ff6");
        WxMpService wxMpService = new WxMpServiceImpl();
        wxMpService.setWxMpConfigStorage(wxStorage);

        WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder()
                .toUser("oozcj58XOWqqRsQokkK5w5M3MOIk")
                .templateId("pZ2aKZjmQqVR1Qxxfi7at8jSscPokW2ZitbCWbsUqBE")
                .build();
        templateMessage.addData(new WxMpTemplateData("data", dateNow));
        templateMessage.addData(new WxMpTemplateData("week", tianQi.getWeek()));
        templateMessage.addData(new WxMpTemplateData("tianqi", tianQi.getWeather(), "#00FFFF"));
        templateMessage.addData(new WxMpTemplateData("tainshu", tainshu));
        templateMessage.addData(new WxMpTemplateData("weather", tianQi.getWeather(), "#FF6347"));
        templateMessage.addData(new WxMpTemplateData("lowest", tianQi.getLowest(), "#FF69B4"));
        templateMessage.addData(new WxMpTemplateData("highest", tianQi.getHighest(), "#FF1493"));
        templateMessage.addData(new WxMpTemplateData("wind", tianQi.getWind(), "#FFA500"));
        templateMessage.addData(new WxMpTemplateData("sunset", tianQi.getSunset(), "#C71585"));
        templateMessage.addData(new WxMpTemplateData("tips", tianQi.getTips(), "#FF6347"));
        templateMessage.addData(new WxMpTemplateData("caihongpi", caihongpi, "#FF69B4"));
        try {
            wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage);
        } catch (Exception e) {
            System.out.println("推送失败：" + e.getMessage());
            e.printStackTrace();
        }
        return R.ok("推送成功");
    }

    /**
     * 彩虹屁api调用
     *
     * @param url
     * @return
     */
    public String getCaiHongPi(String url) {
        String reselt = HttpUtils.get(url);
        JSONObject jsonObject = JSONObject.parseObject(reselt);
        JSONArray newslist = jsonObject.getJSONArray("newslist");
        JSONObject jsonObject1 = newslist.getJSONObject(0);
        return jsonObject1.getString("content");
    }

    /**
     * 获取当前深圳天气
     *
     * @param dateNow
     * @return
     */
    public tianqinewslist getTianQi(String dateNow) {
        String reselt = HttpUtils.get(tianqiurl);
        JSONObject jsonObject = JSONObject.parseObject(reselt);
        JSONArray newslist = jsonObject.getJSONArray("newslist");
        List<tianqinewslist> tianqinewslists = JSON.parseArray(JSON.toJSONString(newslist), tianqinewslist.class);
        List<tianqinewslist> todayTianqi = tianqinewslists.stream().filter(p -> p.getDate().equals(dateNow)).collect(Collectors.toList());
        return todayTianqi.get(0);
    }

    /**
     * 计算在一起天数
     */
    public String tainshu(String dateNow) {
        DateFormat dft = new SimpleDateFormat("yyyy-MM-dd");
        String s = null;
        try {
            Date star = dft.parse("2021-11-03");//开始时间
            Date endDay = dft.parse(dateNow);//结束时间
            Long starTime = star.getTime();
            Long endTime = endDay.getTime();
            Long num = endTime - starTime;//时间戳相差的毫秒数
            Long n = num / 24 / 60 / 60 / 1000;
            s = n.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return s;
    }
}
